<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>配水结果</title>
	
    <link href="/include/css/smoothness/jquery-ui-1.10.3.custom.min.css" rel="stylesheet">
	<style type="text/css">
	.mytable {
	    width: 100%;
	    padding: 0;
	    margin: 0 auto;
	    text-algin: center;
	}
	caption {
	    padding: 0 0 5px 0;
	    font: italic 11px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
	    text-align: center;
	}
	th {
	    font: bold 11px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
	    color: #4f6b72;
	    border-right: 1px solid #C1DAD7;
	    border-bottom: 1px solid #C1DAD7;
	    border-top: 1px solid #C1DAD7;
	    letter-spacing: 2px;
	    text-transform: uppercase;
	    text-align: left;
	    padding: 6px 6px 6px 12px;
	    background-color: #CAE8EA;
	}
	td {
	    border-right: 1px solid #C1DAD7;
	    border-bottom: 1px solid #C1DAD7;
	    background: #fff;
	    font-size:11px;
	    padding: 6px 6px 6px 12px;
	    color: #4f6b72;
	}
	td.alt {
	    background: #F5FAFA;
	    color: #797268;
	}
	/*---------for IE 5.x bug*/
	html>body td{ font-size:11px;}
	
	/*提示圆角框*/
	.circle_bot { clear:both; font: 12px/1.125 Arial; }
	.circle_bot .s_b b { font-size:1px; line-height:1px; overflow:hidden; display:block; clear:both; }
	.circle_bot .s_b b, .circle_bot .info { background:#fff2f2; border:#f88 solid; }
	.circle_bot b.b1 { border-width:1px 0 0 0; margin:0 2px; height:0px; }
	.circle_bot b.b2 { border-width:0 1px; margin:0 1px; height:1px; }
	.circle_bot .info { border-width:0 1px; color:#333; padding:10px; text-align:center;}
	</style>
	
	<script type="text/javascript" src="/include/js/jquery-1.9.1.js"></script>
	<script type="text/javascript" src="/include/js/jquery-ui-1.10.3.custom.min.js"></script>
	<script type="text/javascript" src="/include/js/jquery.ui.datepicker-zh-cn.js"></script>
	<script type="text/javascript">
	var chech_new_loop;
	var wanfa_type;
	var check_new = function(){
		$.getJSON("/view/hasnewpaired.php?wtype=" + wanfa_type + '&' +Date.parse(new Date()), function(re){
			if (re.re == 'succ'){
				$("#reminder").show();
				$("#msgsound").html('<embed src="/include/global.wav" autostart="true" hidden="true">');
				window.clearInterval(chech_new_loop);
			}
		});
	}
	$(function(){
		$("#switch_fetch").click(function(){
			var starting = $(this).attr("starting");
			var setval = (starting == 'y') ? "stop" : "start";
			$.ajax({
				type: "post",
				url: "setfc.php",
				data: {set: setval},
				dataType: "json",
				success: function(data, textStatus){
					if (data.re == 'succ'){
						if (setval == 'start'){
							$("#switch_fetch").val("停止抓取").attr("starting", "y");
							chech_new_loop = window.setInterval('check_new()', 1000);
						}else{
							$("#switch_fetch").val("开始抓取").attr("starting", "n");
							window.clearInterval(chech_new_loop);
						}
					}
					alert(data.msg);
				},
				error: function(){
					alert('设置失败，请重试，再不行找小黑');
				}
			});
		});

	    $( "#datepicker" ).datepicker({
	        buttonImage: "images/calendar.gif",
	        buttonImageOnly: true,
	        changeMonth: true,
	        changeYear: true
	    });
	    
	});
	</script>
</head>

<body>
<?php
require dirname(__FILE__) . '/checkuser.php';
require dirname(dirname(__FILE__)) . '/include/conf.php';
require dirname(dirname(__FILE__)) . '/include/database.php';

$wanfa_type = isset($_GET['wtype']) ? $_GET['wtype'] : 'jiaoqiupan';
$checknew = (isset($_GET['checknew']) && $_GET['checknew'] == 'y') ? $_GET['checknew'] : 'n';
$opt = isset($_GET['opt']) ? $_GET['opt'] : '';
$date = isset($_GET['date']) ? $_GET['date'] : date('Y-m-d');
$opt_set = isset($_POST['opt']) ? $_POST['opt'] : '';
$date_set = isset($_POST['date']) ? $_POST['date'] : '';
$wtype_set = isset($_POST['wtype']) ? $_POST['wtype'] : 'jiaoqiupan';

// 检查参数合法性
if (strlen($date) != 10){
	exit('parameter [date] error!');
}
if (!empty($date_set) && strlen($date_set) != 10){
	exit('parameter [date_set] error!');
}

$db = db_touzhu();
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
	if ($opt_set == 'setre'){
		// 设置比赛结果
		foreach ($_POST as $_key => $per){
			$_tmp_arr = explode('_', $_key);
			if ($_tmp_arr[0] == 'matchre' && $per != '' && isset($_tmp_arr[1]) && isset($_tmp_arr[2])){
				$_tmp_arr[1] = (int) $_tmp_arr[1];
				$_tmp_arr[2] = (int) $_tmp_arr[2];
				$per = (int) $per;

				if ($wtype_set == 'jiaoqiupan'){
					$up_where .= ' and wanfa in(1,2) ';
				}else {
					$up_where .= ' and wanfa in(3,4) ';
				}
				$sql_upre = "update match_paired set match_result={$per} where home_team_id={$_tmp_arr[1]} and visiting_team_id={$_tmp_arr[2]} and fetch_time>='{$date_set} 00:00:00' and fetch_time<='{$date_set} 23:59:59'{$up_where}";
				$db->query($sql_upre);
			}
		}
	}else {
		// 设置配水参数
		$profit_rate = (int) $_POST['profit_rate'];
		$money_188 = (int) $_POST['money_188'];
		$same_times = (int) $_POST['same_times'];
		$reminder = (isset($_POST['reminder']) && $_POST['reminder'] == 'on') ? 1 : 0;
		
		if ($money_188 <= 0){
			exit('<span style="color:red">金宝博每次出手金额错误</span>，<a href="javascript: history.go(-1);">返回</a>重新设置');
		}
		if ($same_times <= 0){
			exit('<span style="color:red">相同盘口最多出现次数错误</span>，<a href="javascript: history.go(-1);">返回</a>重新设置');
		}
		$set_vals = array(
			'profit_rate' => $profit_rate,
			'money_188' => $money_188,
			'same_times' => $same_times,
			'reminder' => $reminder
		);
		$re = conf_pair_sets('set', $wanfa_type, $set_vals);
		if ($re === false){
			exit('<span style="color:red">设置失败</span>，<a href="javascript: history.go(-1);">返回</a>重新设置，再不行就告诉小黑吧');
		}
	}
}

// 读取设置参数
$set_vals = conf_pair_sets('get', $wanfa_type);
if ($set_vals !== false){
	$profit_rate = $set_vals['profit_rate'];
	$money_188 = $set_vals['money_188'];
	$same_times = $set_vals['same_times'];
	$reminder = $set_vals['reminder'];
}else {
	echo '<span style="color:red">未设置过配水参数或读取配置失败，使用如下默认值！</span>';
	$profit_rate = 0;
	$money_188 = 1000;
	$same_times = 5;
	$reminder = 1;
}

// 是否正在抓取
$set_str = conf_switch_sets('get');
if ($set_str == 'start'){
	$starting = 'y';
	$fc_btn_str = '停止抓取';
}else {
	$starting = 'n';
	$fc_btn_str = '开始抓取';
}

if ($reminder == 1 && $starting == 'y'){
	echo '<script>wanfa_type="' . $wanfa_type . '";chech_new_loop = window.setInterval("check_new()", 1000);</script>';
}
?>

<div id="reminder" style="display: none">
	<div id="msgsound"></div>
	<div class="circle_bot">
		<span class="s_b"><b class="b1"></b><b class="b2"></b></span>
		<div class="info"><a href="?checknew=y&wtype=<?php echo $wanfa_type;?>">有新条目</a></div>
		<span class="s_b"><b class="b2"></b> <b class="b1"></b></span>
	</div>
</div>
<div style="margin: 20px;">
	<?php if ($opt == 'setre'){ ?>
	<div>
		<form action="" method="get">
		<input type="hidden" name="opt" value="setre" />
		<input type="hidden" name="wtype" value="<?php echo $wanfa_type?>" />
		日期：<input type="text" id="datepicker" name="date" value="<?php echo $date?>" />
		<input type="submit" value=" 查 询 " />
		</form>
	</div>
	<?php }else {?>
	<form action="" method="post">
	<table>
		<tr>
			<td align="right">获利率大于千分之多少才配水： </td>
			<td align="left"><input type="input" name="profit_rate" value="<?php echo $profit_rate?>" size="5"></td>
			<td rowspan="4" width="250px" align="center">
				<input type="button" value="<?php echo $fc_btn_str?>" starting="<?php echo $starting?>" id="switch_fetch" style="width: 100px;height: 50px;font-size: 18px;font-weight:bold">
				<br/><br/><a href="paired.php?opt=setre&wtype=<?php echo $wanfa_type?>">设置结果</a>
			</td>
		</tr>
		<tr>
			<td align="right">金宝博每次出手金额：</td>
			<td align="left"><input type="input" name="money_188" value="<?php echo $money_188?>" size="5"></td>
		</tr>
		<tr>
			<td align="right">相同盘口最多出几次： </td>
			<td align="left"><input type="input" name="same_times" value="<?php echo $same_times?>" size="5"></td>
		</tr>
		<tr>
			<td align="right"><label><input type="checkbox" name="reminder" <?php echo $reminder ? 'checked="true"' : ''?>>提示音</label></td>
			<td><input type="submit" value=" 保 存 " style="width: 80px;height: 30px;font-size: 16px"></td>
		</tr>
	</table>
	</form>
	<?php }?>
</div>

<form action="" method="post">
<table class="mytable" cellspacing="0">
<caption>比赛列表</caption>
  <tr>
	<th>比赛时间</th>
	<th>联赛名称</th>
	<th>主队</th>
	<th>客队</th>
	<th>玩法</th><!-- 上半场亚洲盘角球 | 亚洲盘角球 -->
	<th>盘口</th>
	<th>365大水</th><!-- 赔率 -->
	<th>365小水</th>
	<th>188大水</th>
	<th>188小水</th>
	<th colspan="2" style="text-align: center;">配对</th>
	<th>可赢</th>
	<th>获利率</th>
	<th>抓取时间</th>
	<?php if($opt == 'setre') echo '<th>结果</th>'?>
  </tr>
  
<?php
$start_time = date('Y-m-d H:i:s', time() - 60 * 60 * 24);
if ($opt == 'setre'){
	$where = "where fetch_time>='{$date} 00:00:00' and fetch_time<='{$date} 23:59:59' ";
}else {
	$where = "where fetch_time>='{$start_time}' ";
}
if ($wanfa_type == 'jiaoqiupan'){
	$where .= ' and wanfa in(1,2) ';
}else {
	$where .= ' and wanfa in(3,4) ';
}
$sql = "select * from match_paired {$where} order by id desc";
$re = $db->query($sql);

if (is_array($re) && !empty($re)){
	$match_groups = array();
	$upstatus = false;
	if ($opt == 'setre'){
		foreach ($re as $v){
			$_key = "{$v['home_team_id']}_{$v['visiting_team_id']}";
			if (!isset($match_groups[$_key])){
				$match_groups[$_key][0] = $v;
			}
		}
	}else {
		foreach ($re as $v){
			$_key = "{$v['home_team_id']}_{$v['visiting_team_id']}_{$v['pankou']}";
			if ($v['profit_rate'] >= $profit_rate){
				if (isset($match_groups[$_key])){
					if (count($match_groups[$_key]) < $same_times){
						$match_groups[$_key][] = $v;
					}
				}else {
					$match_groups[$_key][] = $v;
				}
			}
			if ($v['status'] == 0 && !$upstatus){
				$upstatus = true;
			}
		}
		
		// 更新status
		if ($checknew == 'y' && $upstatus){
			$sql_upstatus = 'update match_paired set status=1 where status=0';
			$re_up = $db->query($sql_upstatus);
			if (!$re_up){
				$db->query($sql_upstatus);
			}
		}
	}

	if (!empty($match_groups)){
		$counter = 1;
		foreach ($match_groups as $match_group){
			foreach ($match_group as $match){
				$_earn = '<span style="color:green">'.$match['money_earn'].'</span>';
				if ($match['bet_big'] == 1){
					$_money_365 = '365大 ' . $match['money_365'];
					$_money_188 = '188小 ' . $match['money_188'];
				}else {
					$_money_365 = '365小 ' . $match['money_365'];
					$_money_188 = '188大 ' . $match['money_188'];
				}
				echo '<tr>';
				echo '<td',  ($counter % 2 == 0) ? ' class="alt"' : '', '>', $match['status'] == 0 ? '<span style="color:red">NEW</span> ' : '', $match['match_time'], '</td>';
				echo '<td',  ($counter % 2 == 0) ? ' class="alt"' : '', '>', $match['match_name'], '</td>';
				echo '<td',  ($counter % 2 == 0) ? ' class="alt"' : '', '>', $match['home_team'], '</td>';
				echo '<td',  ($counter % 2 == 0) ? ' class="alt"' : '', '>', $match['visiting_team'], '</td>';
				echo '<td',  ($counter % 2 == 0) ? ' class="alt"' : '', '>', $GLOBAL_WANFA_NUM[$match['wanfa']], '</td>';
				echo '<td',  ($counter % 2 == 0) ? ' class="alt"' : '', '>', $match['pankou'], '</td>';
				echo '<td',  ($counter % 2 == 0) ? ' class="alt"' : '', '>', $match['over_365'], '</td>';
				echo '<td',  ($counter % 2 == 0) ? ' class="alt"' : '', '>', $match['under_365'], '</td>';
				echo '<td',  ($counter % 2 == 0) ? ' class="alt"' : '', '>', $match['over_188'], '</td>';
				echo '<td',  ($counter % 2 == 0) ? ' class="alt"' : '', '>', $match['under_188'], '</td>';
				echo '<td',  ($counter % 2 == 0) ? ' class="alt"' : '', '>', $_money_365, '</td>';
				echo '<td',  ($counter % 2 == 0) ? ' class="alt"' : '', '>', $_money_188, '</td>';
				echo '<td',  ($counter % 2 == 0) ? ' class="alt"' : '', '>', $_earn, '</td>';
				echo '<td',  ($counter % 2 == 0) ? ' class="alt"' : '', '>', $match['profit_rate'], '</td>';
				echo '<td',  ($counter % 2 == 0) ? ' class="alt"' : '', '>', $match['fetch_time'], '</td>';
				if($opt == 'setre') echo '<td style="padding-top:2px;padding-bottom:2px"><input type="text" name="matchre_' . $match['home_team_id'] . '_' . $match['visiting_team_id'] . '" value="' . ($match['match_result'] > -1 ? $match['match_result'] : '') . '" size="1" /></td>';
				echo "</tr>\n";
				$counter ++;
			}
		}
	}
	
}
?>
<?php if($opt == 'setre') echo '<tr><td colspan="16" align="right"><input type="submit" value=" 保 存 " /></td></tr>',
'<input type="hidden" name="opt" value="setre" /><input type="hidden" name="wtype" value="' . $wanfa_type . '" /><input type="hidden" name="date" value="' . $date . '" />'?>
</table>
</form>
</body>
</html>
